package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
import com.apps.ftapp.R;
import com.apps.ftapp.model.LianXiModel;
import com.apps.ftapp.util.Tools;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lyli on 2017/10/6.
 */

public class LianXiAdapter extends BaseAdapter {

    private Context context;
    private List<LianXiModel.ChildQuestionCategoryBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;


    public LianXiAdapter(Context context, List dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<LianXiModel.ChildQuestionCategoryBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LianXiModel.ChildQuestionCategoryBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_lianxi_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.lianxiTitle.setText(bean.getName());
        viewHolder.lianxiRight.setText(bean.getDo_counts() + "/" + bean.getCounts());
        viewHolder.lianxiStar.setRating(bean.getXingji());
        //  viewHolder.lianxiJindu.setProgress(bean.getCounts());


        viewHolder.lianxiJindu.setMax(Tools.isFloatNull(bean.getCounts()) == 0 ? 1 : Tools.isFloatNull(bean.getCounts()));
        viewHolder.lianxiJindu.setProgress(Tools.isFloatNull(bean.getDo_counts()) == 0 ? 1 : Tools.isFloatNull(bean.getDo_counts()));


        return view;
    }


    class ViewHolder {
        ImageView lianxiEdit;
        TextView lianxiTitle;
        SimpleRatingBar lianxiStar;
        TextView lianxiRight;
        RoundCornerProgressBar lianxiJindu;

        public ViewHolder(View view) {
            lianxiEdit = view.findViewById(R.id.lianxi_edit);
            lianxiTitle = view.findViewById(R.id.lianxi_title);
            lianxiStar = view.findViewById(R.id.lianxi_star);
            lianxiRight = view.findViewById(R.id.lianxi_right);
            lianxiJindu = view.findViewById(R.id.lianxi_jindu);
        }
    }

}
